<!--
 * @Author: Songjq
 * @Date: 2022-03-30 21:14:43
 * @Desscription: 
-->
<template>
  <div>
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" placeholder="请输入短域名">
        <template v-slot:left>
          <el-select
            size="mini"
            v-model="query.type"
            placeholder="类型"
            @change="crud.toQuery()"
            clearable
            filterable
            class="ui-w-120"
          >
            <el-option
              v-for="item in dict.long_url_type"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </template>
        <template v-slot:right />
      </crudOperation>

      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="660px"
        :append-to-body="true"
      >
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          size="small"
          label-width="100px"
        >
          <el-form-item
            v-permission="permission.write"
            label="绑定用户"
            prop="user"
          >
            <!-- 用户 -->
            <UserSelect
              ref="user-select"
              class="w-100"
              :queryParam="form"
              placeholder="绑定用户"
            />
          </el-form-item>
          <el-form-item label="短域名" prop="shortUrl">
            <el-input
              size="small"
              :disabled="!checkPermission(permission.write)"
              v-model="form.shortUrl"
              placeholder="请输入短域名"
            ></el-input>
          </el-form-item>
          <el-form-item label="类型" prop="type">
            <el-select
              v-model="form.type"
              placeholder="请选择"
              class="w-100"
              :disabled="!checkPermission(permission.write)"
            >
              <el-option
                v-for="item in dict.long_url_type"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="长链接" prop="longUrlLists">
            <div class="dialogLong">
              <template v-for="(item, index) in form.longUrlLists">
                <div class="ui-flex ui-items-center long" :key="index">
                  <el-input
                    :disabled="!checkPermission(permission.write)"
                    v-model="item.label"
                    size="small"
                    class="ui-w-130 ui-mgr-6"
                    placeholder="标题"
                  />
                  <el-input
                    :disabled="!checkPermission(permission.write)"
                    v-model="item.className"
                    size="small"
                    class="ui-w-100 ui-mgr-6"
                    placeholder="类名"
                  />
                  <el-input
                    v-model="item.url"
                    size="small"
                    class="ui-flex-1"
                    placeholder="请输入对应长链接"
                  />
                  <i
                    v-permission="permission.write"
                    class="
                      el-icon-circle-plus
                      ui-size-18 ui-cous ui-mgl-6
                      changebtn
                    "
                    @click="addLongUrl(index)"
                  ></i>
                  <i
                    v-permission="permission.write"
                    class="el-icon-remove ui-size-18 ui-cous ui-mgl-6 changebtn"
                    @click="reduceLongUrl(index)"
                  ></i>
                </div>
              </template>
            </div>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <template v-for="(item, idx) in dict.sys_disabled_status">
              <el-radio :key="idx" v-model="form.status" :label="item.value">{{
                item.label
              }}</el-radio>
            </template>
          </el-form-item>
          <el-form-item label="过期时间" prop="expirationTime">
            <el-date-picker
              :disabled="!checkPermission(permission.write)"
              type="date"
              size="small"
              placeholder="选择日期"
              v-model="form.expirationTime"
              style="width: 100%"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="描述" prop="desc">
            <el-input type="textarea" v-model="form.desc"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button
            :loading="crud.btnLoading"
            type="primary"
            @click="crud.submitCU"
            >确认</el-button
          >
        </span>
      </el-dialog>

      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        style="width: 100%"
        border
        :default-sort="{ prop: 'id', order: 'descending' }"
        height="calc(100vh - 180px)"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="40" />
        <el-table-column label="序号" type="index"> </el-table-column>
        <el-table-column prop="shortUrl" label="短域名" sortable width="120">
          <template slot-scope="scope">
            <a :href="'http://' + scope.row.shortUrl" target="blank">
              {{ scope.row.shortUrl }}
            </a>
          </template>
        </el-table-column>
        <el-table-column prop="type" label="类型" sortable width="80">
          <template slot-scope="scope">
            {{ getPubTypeStr(scope.row.type, dict.long_url_type) }}
          </template>
        </el-table-column>
        <el-table-column prop="longUrlLists" label="长链接 (点击可直接测试)">
          <template slot-scope="scope">
            <div class="longUrlLists">
              <div
                class="ui-flex ui-pdt-6 ui-pdb-6 ui-items-center"
                v-for="(item, index) in scope.row.longUrlLists"
                :key="index"
              >
                <span style="font-weight: bold">{{ item.label }}：</span>
                <span>
                  <a :href="item.url" target="blank" style="color: #0083ff">
                    {{ item.url }}
                  </a>
                </span>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="150">
          <template slot-scope="scope">
            <disabled-tag
              :status="scope.row.status"
              :options="dict.sys_disabled_status"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="expirationTime"
          label="过期日期"
          sortable
          width="150"
        >
          <template slot-scope="scope">
            {{ parseTime(scope.row.expirationTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="desc" label="描述" width="180">
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="60">
          <template slot-scope="scope">
            <udOperation :data="scope.row" :permission="permission" />
          </template>
        </el-table-column>
      </el-table>

      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import { parseTime } from "@/utils";
import DNS from "@http/domain/dns.js";
import CRUD, { presenter, header, form, crud } from "@crud/crud";
import crudOperation from "@crud/CRUD.operation";
import udOperation from "@crud/UD.operation";
import pagination from "@crud/Pagination";
import publice from "@/mixins/publice.js";

// crud交由presenter持有
const defaultCrud = CRUD({
  title: "长链接",
  url: "api/dns/all",
  crudMethod: { ...DNS },
  optShow: { user: true, status: true, add: true, del: true, download: true },
});

const defaultForm = {
  _id: null,
  user: "",
  shortUrl: "",
  type: "",
  longUrlLists: [
    {
      label: "跳转",
      className: "",
      url: "",
      des: "",
    },
  ],
  status: "0",
  expirationTime: null,
  desc: "",
};

export default {
  name: "DNS",
  components: { pagination, crudOperation, udOperation },
  mixins: [
    presenter(defaultCrud),
    header(),
    form(defaultForm),
    crud(),
    publice,
  ],
  dicts: ["long_url_type", "sys_disabled_status"],
  data() {
    return {
      permission: {
        user: ["admin", "dns:user"],
        list: ["admin", "dns:list"],
        add: ["admin", "dns:add"],
        edit: ["admin", "dns:edit"],
        del: ["admin", "dns:del"],
        write: ["admin", "dns:write"],
        export: ["admin", "dns:export"],
      },
      rules: {
        user: [{ required: true, message: "请绑定用户", trigger: "change" }],
        shortUrl: [
          { required: true, message: "请输入短域名", trigger: "blur" },
        ],
        type: [{ required: true, message: "请选择类型", trigger: "change" }],
        expirationTime: [
          {
            required: true,
            message: "请选择日期",
            trigger: "change",
          },
        ],
      },
    };
  },
  computed: {},
  watch: {},
  created() {},
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      return true;
    },

    /**
     * @Author: Songjq
     * @Date: 2021-12-08 23:14:45
     * @Fn:增加长链接
     */
    addLongUrl(idx) {
      this.form.longUrlLists.splice(idx + 1, 0, {
        label: "",
        className: "",
        url: "",
        des: "",
      });
    },

    // 减少
    reduceLongUrl(idx) {
      this.form.longUrlLists.splice(idx, 1);
    },
  },
};
</script>
<style lang="scss" scoped></style>